package com.wavehi.consumer;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description 消费者
 * @Author WaveHi
 * @Date 24/12/2021 下午 2:54
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 */

public class Consumer_01 {

    //队列名称
    private final static String QUEUE_NAME = "Hello";

    public static void main(String[] args) throws IOException, TimeoutException {

        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //2.连接RabbitMQ
        connectionFactory.setHost("192.168.13.131");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123");
        //3.创建连接
        Connection connection = connectionFactory.newConnection();
        //4.创建信道
        Channel channel = connection.createChannel();
        //5.接收消息
        //5.1.准备消息的消费操作
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println(new String(message.getBody()));
        };
        //5.2.准备消息消费中断后的操作
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("Message consume stop!");
        };
        //5.3.接收消息
        //参数说明
        //  1.消费哪个队列
        //  2.消费成功后是否自动应答
        //  3.消费操作
        //  4.消费中断的回调操作
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);

    }

}
